// pages/getUserInfo/getUserInfo.js
import Dialog from '@vant/weapp/dialog/dialog';
let app = getApp();
const Multipart = require('../../utils/Multipart.min.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cw: "",
    ch: "",
    host: app.globalData.host,
    userInfo: "",
    avatarUrl: wx.getStorageSync('userInfo').avatarUrl,
    avatarFlag: false,
    nickName: "",
  },

  onChooseAvatar(e) {
    console.log(e.detail)
    this.setData({
      avatarFlag: true,
      avatarUrl: e.detail.avatarUrl
    })
  },

	formsubmit(e){
    console.log('nickName', e.detail.value.nickname)
    this.setData({
      nickName: e.detail.value.nickname
    })
    this.submit()
	},
  
  submit() {
    let that = this
    if (!this.data.nickName) {
      return
    }

    let fields =[{
      name:'nickname',
      value: this.data.nickName
    }]

    let files = []
    if (this.data.avatarFlag) {
      files.push( {
        filePath: this.data.avatarUrl, //value
        name: 'avatar' //key
      })
    }

    console.info(fields, files)
    new Multipart({
      fields, //非文件参数
      files, //文件参数
    }).submit(this.data.host + wx.api.complete, {
      header: {
        "token": app.globalData.token || wx.getStorageSync('token')
      }
    }).then (res=> {
      wx.hideLoading()
      if (res.data.code == 200 || res.data.code == 2039 || res.data.code == 2040) {
        console.log('==== add ===', res)
        Dialog.alert({
          title: '温馨提示',
          message: res.data.msg,
          confirmButtonText: '好的'
        }).then(() => {
          let userInfo = {
            ...this.data.userInfo,
            avatar: res.data.data.avatar,
            nickname: res.data.data.nickname
          }
          getApp().globalData.userInfo = userInfo
          wx.setStorage({
            key:"userInfo",
            data: userInfo,
            success() {
              that.back()
            }
          })
        })
      } else if (res.data.code == 2002) {
        app.globalData.userInfo = {}
        app.globalData.token = ''
        wx.removeStorage({
          key: 'userInfo'
        })
        wx.removeStorage({
            key: 'token'
        })
        wx.showToast({
          title: '登录过期，请重新登录',
          icon: 'none',
          mask: true,
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }, 2000)
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        })
        wx.hideLoading()
      }
    })
  },

  back() {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo || wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})